package com.hjx.face.javacv.face;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;


import java.awt.BorderLayout;
import java.awt.Color;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.border.EtchedBorder;

import com.hjx.face.javacv.face.JavacvService.FaceListener;
import com.hjx.face.javacv.face.KeyboardHook.KeyboardHookIntercept;
import com.hjx.face.javacv.face.MouseHook.MouseHookIntercept;

public class FaceRecognitionApp {

	private JFrame frame;
	JavacvService javacvService;
	FaceRecognizerService faceRecognizerService;
	private JPanel p_player;
	private JLabel lbl_matchRatio;
	private JLabel lbl_matchTime;
	private JPanel face_now;
	private JPanel face_source;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					FaceRecognitionApp window = new FaceRecognitionApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Create the application.
	 */
	public FaceRecognitionApp() {
		this(new JavacvService(),new FaceRecognizerService());
	}
	public FaceRecognitionApp(JavacvService javacvService,FaceRecognizerService faceRecognizerService) {
		this.javacvService = javacvService;
		this.faceRecognizerService = faceRecognizerService;
		initialize();
	}
	DateFormat df=new SimpleDateFormat("MM-dd HH:mm:ss:SSS");
	
	private boolean isHook=false;
	private JButton btn_lockScreen;
	private JButton btn_start;
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 787, 560);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		frame.setAlwaysOnTop(true);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				javacvService.stop();
			}
		});
		
		btn_start = new JButton("开始");
		btn_start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				start();
			}
		});
		panel.add(btn_start);
		
		btn_lockScreen = new JButton("锁屏");
		btn_lockScreen.setEnabled(false);
		btn_lockScreen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lockScren();
			}
		});
		panel.add(btn_lockScreen);
		
		JButton button_1 = new JButton("退出");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		panel.add(button_1);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		p_player = new JPanel();
		p_player.setBounds(133, 10, 640, 480);
		panel_1.add(p_player);
		
		face_source = new JPanel();
		face_source.setBounds(10, 10, 100, 100);
		panel_1.add(face_source);
		
		face_now = new JPanel();
		face_now.setBounds(10, 132, 100, 100);
		panel_1.add(face_now);
		
		lbl_matchTime = new JLabel("07-11 11:11:11:111");
		lbl_matchTime.setFont(new Font("宋体", Font.PLAIN, 9));
		lbl_matchTime.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		lbl_matchTime.setBounds(10, 242, 100, 24);
		panel_1.add(lbl_matchTime);
		
		lbl_matchRatio = new JLabel("80%");
		lbl_matchRatio.setFont(new Font("宋体", Font.PLAIN, 9));
		lbl_matchRatio.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		lbl_matchRatio.setBounds(10, 277, 100, 24);
		panel_1.add(lbl_matchRatio);
		faceRecognizerService.init();
		javacvService.start();
		init();
	}

	private void init() {
		boolean [] on_off={true};
		KeyboardHook keyboardHook = new KeyboardHook(on_off);
		keyboardHook.setIntercept(new KeyboardHookIntercept() {
			Map<Integer, Integer> mapLastKeyDown=new HashMap<>();
			@Override
			public boolean check(int keyType,int vkCode) {
				System.out.println((char)vkCode);
				return false;
			}
		});
		new Thread(keyboardHook).start();
		MouseHook mouseHook = new MouseHook(on_off);
		mouseHook.intercept=new MouseHookIntercept() {
			@Override
			public boolean check(int button, int x, int y) {
				if (button==MouseHook.WM_MOUSEMOVE||!isHook) {
					return false;
				}
//				Point lm = frame.getLocation();
//				Point lb = btn_start.getLocation();
//				int width = btn_start.getWidth();
//				int height = btn_start.getHeight();
//				int xMin=lm.x+lb.x;
//				int yMin=lm.y+lb.y;
//				int xMax=xMin+width;
//				int yMax=yMin+height+30;
//				System.out.println(lm+""+lb+"==xMin=="+xMin+"==yMin=="+yMin+"==yMin=="+xMax+"==yMax=="+yMax+"");
//				if (x>xMin&&x<xMax&&y>yMin&&y<yMax) {
//					System.out.println(false);
//				}
				return true;
			}
		};
		new Thread(mouseHook).start();
	}

	public JFrame getFrame() {
		return frame;
	}

	/**
	 * 
	 */
	public void lockScren() {
		isHook=true;
		btn_lockScreen.setEnabled(false);
		face_now.repaint();
		face_source.repaint();
	}

	/**
	 * 
	 */
	public void start() {
		javacvService.play(p_player);
		javacvService.addFaceListener(new FaceListener() {
			int i=0;
			@Override
			public void call(Graphics2D g, BufferedImage faceImage, Rectangle rectangle) {
				try {
					faceRecognizerService.predict(faceImage);
					g.setFont(new Font("微软雅黑", 0, 15));
					g.setColor(Color.YELLOW);
					double matchRatio = faceRecognizerService.getMatchRatio();
					String name = faceRecognizerService.getName();
					g.drawString(name+"="+matchRatio+"%", rectangle.x, rectangle.height+15);
					if (matchRatio>90) {
						isHook=false;
						btn_lockScreen.setEnabled(true);
						String faceSource=JavacvService.FACE_SAVE_DIR+File.separator+name+".jpg";
						JavacvService.setImage(face_now, faceImage);
						lbl_matchTime.setText(df.format(new Date()));
						JavacvService.setImage(face_source, ImageIO.read(new File(faceSource)));
						lbl_matchRatio.setText(matchRatio+"%");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btn_start.setEnabled(false);
		btn_lockScreen.setEnabled(true);
	}
}
